import java.io.*;
import java.net.*;
import java.util.*;
import java.security.Security;
import java.security.Provider;
import javax.net.ssl.*;

public class RealQuote {    

    private static String input = new String();
    private static Vector vInput= new Vector(10);
    private static Vector vOut = new Vector(10);

    public static void main(String[] args) {
	String url = "https://investment.datek.com/?streamer";
	RealQuote myQuote = new RealQuote();
	myQuote.connectToUrl(url);
	//	vInput = myQuote.putInVectorByLine(input);
	//myConnecter.printVector(vInput);
	//vOut = myQuote.selectUsefulInfo(vInput);
	//myConnecter.printVector(vOut);
	//myQuote.printSambolSid(vOut); 
    }



    private Vector selectUsefulInfo (Vector v_in) {
	Vector v_out = new Vector (10);
	String text = "";
	Enumeration penum = v_in.elements();
	while ( penum.hasMoreElements() ) {
	    text = (String)penum.nextElement();
	    if (text.startsWith("<A HREF=\"/custom/datek-com/screener/screener-charting.asp?symb=")) {
		v_out.addElement(text);
	    }
	}

	return v_out;
    }

    private void printUsefulInfo (String input) {
	System.out.print(input);
    }

    private void printVector( Vector inputV) {

	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements() ) {   
	   System.out.println(penum.nextElement()+"\n");
	}
    }

    private void printSambolSid( Vector inputV) {
	String temp = "";
	Enumeration penum = inputV.elements();
	while ( penum.hasMoreElements() ) {
	    temp = (String)penum.nextElement();
	    temp = temp.replace('?', ' ');
	    temp = temp.replaceAll("<A HREF=","");
	    temp = temp.replaceAll("/custom/datek-com/screener/screener-charting.asp", "");
	    temp = temp.replaceAll("symb=","");
	    temp = temp.replaceAll("\"","");
	    temp = temp.replaceAll("&sid=", "\t");
	    temp = temp.replaceAll(">","");  
	   System.out.println(temp.trim());
	}
    }

    private void connectToUrl (String urlAddress) {
        URL url = null;        
	HttpsURLConnection httpsConnection = null;
	String up;
	up =  encodeAuthorization();

	try {            

	    url = new URL (urlAddress);  
	    InputStream inputStr;// = new InputStream();
	    try {                
		httpsConnection = (HttpsURLConnection)url.openConnection();  
		System.out.println("11111111111111");              
		//httpsConnection.setDoOutput(true);                
		httpsConnection.setRequestProperty("Authorization", up);
		//httpsConnection.setRequestMethod("POST");
		//httpsConnection.setRequestProperty("", "Get Quote");
		//httpsConnection.setRequestProperty("submit", "Go");
		//httpsConnection.setRequestProperty("userid", "LI34276");
		//httpsConnection.setRequestProperty("Password", "LiNgu0578");
		//httpsConnection.setRequestProperty("__chx", "QUOTE");
		//httpsConnection.connect();                
		httpsConnection.connect();
	System.out.println("2222222222");              
	inputStr=httpsConnection.getErrorStream();
	System.out.println(inputStr);              
                
		// get the HTTPS server's response code                
		System.out.println("Connection Code and Message:" + 
				   httpsConnection.getResponseCode() + 
				   " - " + httpsConnection.getResponseMessage()+"\n");

                // get the input stream of the connection                
		// ie. read the response from the HTTPS server
                
		BufferedReader in = new BufferedReader(new InputStreamReader
		    (httpsConnection.getInputStream()));

		StringBuffer buff1= new StringBuffer("");

		input = in.readLine();

		while (input != null) {
		    buff1.append(input);
		    input = in.readLine();
		}

		in.close();            

		input =  buff1.toString();
		input=input.replaceAll("\n","");
		input=input.replaceAll("\t","");
		input=input.replaceAll("&nbsp;","");
		input=input.replaceAll(">",">\n");
		input=input.replaceAll("</","\n</");

	    } catch (IOException ioe) {                
		System.out.println(ioe.getMessage());  
		System.exit(1);            
	    }        
	} catch (MalformedURLException mue) {            
	    System.out.println(mue.getMessage());            
	    System.exit(1);        
	} catch (Exception e) {            
	    e.printStackTrace();            
	    System.exit(1);        
	}    
    }

    private Vector putInVectorByLine (String input) {

	Vector v = new Vector (10);
	String element = "";
	StringTokenizer tokens = new StringTokenizer(input, "\n");
	while (tokens.hasMoreTokens()) {
	    element = tokens.nextToken();
	    if (element != null) {
		v.addElement(element);
	    }
	}
	
	return v;
    }

    private static String encodeAuthorization () {
	// method to encode the username/password

	String authorization = "SSSSSS" + ":" + "XXXXXX";
	sun.misc.BASE64Encoder enCoder = new sun.misc.BASE64Encoder();
	//return "Basic " + enCoder.encode(authorization.getBytes());
	return enCoder.encode(authorization.getBytes());
    }
}






